<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\func\Tools;
use app\common\model\Address;

/**
 * 订单
 * Class
 * @package
 */
class Order extends Api
{
	protected $noNeedLogin = ['apply'];
	protected $noNeedRight = '*';

	/**
	 * 申领
	 */
	public function apply()
	{
		// $uid = $this->auth->id;
		$uid = 11;
		$proid = $this->request->post("proid");
		$types = $this->request->post("types");
		if (!$proid) {
			$this->error(__('Invalid parameters'));
		}
		$ret = Tools::saveProductApply($proid, $types, $uid, 1);
		if ($ret) {
			$this->success(__('Logged in successful'), 1);
		}
		$this->error("失败");
	}

	/**
	 * 获取收货地址列表
	 */
	public function getAddress() {
		$data = Address::where(["user_id" => $this->auth->id])->order("create_time desc")->select();
		$this->success('收货地址', $data);
	}

	/**
	 * 设置默认收货地址
	 */
	public function setAddressDefault() {
		$id = $this->request->post("address_id");
		if (!$id) {
			$this->error('参数错误');
		}
		Address::where('user_id', $this->auth->id)->update(["default" => 2]);
		Address::where('id', $id)->update(["default" => 1]);
		$this->success('设置成功');
	}

	/**
	 * 添加收货地址
	 */
	public function addAddress() {
		$name = $this->request->post("name");
        $mobile = $this->request->post("mobile");
        $province = $this->request->post("province");
        $city = $this->request->post("city");
        $district = $this->request->post("district");
        $address = $this->request->post("address");
        $default = $this->request->post("default", 2);

        if (!$name || !$mobile) {
            $this->error("缺少联系人信息");
        }

        if (!$province || !$city || !$district || !$address) {
            $this->error("收货地址不能为空");
        }

        $result = false;
		
		if ($default > 0) {
			Address::where('user_id', $this->auth->id)->update(["default" => 2]);
		}

		$data = [
			"user_id" => $this->auth->id,
			"name" => $name,
			"mobile" => $mobile,
			"province" => $province,
			"city" => $city,
			"district" => $district,
			"address" => $address,
			"create_time" => time(),
			"default" => $default > 0 ? 1 : 2
		];
		$result = Address::create($data);
			

        if ($result) {
            $this->success("添加成功！");
        } else {
            $this->error("添加失败！");
        }
	}

	/**
	 * 获取收货地址信息
	 */
	public function getAddressInfo() {
		$id = $this->request->post("id");
		if (!$id) {
			$this->error('参数错误');
		}
		$data = Address::where('id', $id)->find();
		$this->success("收货地址信息", $data);
	}

	/**
	 * 保存收货地址信息
	 */
	public function editAddress() {
		$id = $this->request->post("id");
		$name = $this->request->post("name");
        $mobile = $this->request->post("mobile");
        $province = $this->request->post("province");
        $city = $this->request->post("city");
        $district = $this->request->post("district");
        $address = $this->request->post("address");
        $default = $this->request->post("default", 2);

        if (!$name || !$mobile) {
            $this->error("缺少联系人信息");
        }

        if (!$province || !$city || !$district || !$address) {
            $this->error("收货地址不能为空");
        }

        $result = false;
		
		if ($default > 0) {
			Address::where('user_id', $this->auth->id)->update(["default" => 2]);
		}

		$data = [
			"user_id" => $this->auth->id,
			"name" => $name,
			"mobile" => $mobile,
			"province" => $province,
			"city" => $city,
			"district" => $district,
			"address" => $address,
			"default" => $default > 0 ? 1 : 2
		];
		$result = Address::where('id', $id)->update($data);

        if ($result) {
            $this->success("修改成功！");
        } else {
            $this->error("修改失败！");
        }
	}

	/**
	 * 获取默认收货地址
	 */
	public function getDefaultAddress() {
		$data = Address::where(["user_id" => $this->auth->id, "default" => 1])->field("id,name,insert(mobile , 4 , 4 , '****') mobile,province,city,district,address")->find();
		$this->success("默认地址", $data);
	}
}
